<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 专题控制器
 * @author Ringo Chan <334258471@qq.com>
 */
class SpecialController extends AdminController {
    
    /**
     * 抢购会开启时间表
     * 
     * @author Ringo Chan <334258471@qq.com>
     */
    public function panic(){
        $map['status'] = array('egt', 0);
        $list   = $this->lists('special_panic', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '抢购会时间表';
        $this->display();
    }
    
    /**
     * 新增抢购会时间表
     *
     * @author Ringo Chan <334258471@qq.com>
     */
    public function addpanic(){
        if (IS_POST) {
            // !preg_match('/^\d{4}-\d{2}-\d{2}$/s', I('post.start_time')) && $this->error('初始时间不能为空或初始时间格式不对');
            getStatus('hours', I('post.start_time_hour')) == null && $this->error('请选择初始时间的小时时间段');
            // !preg_match('/^\d{4}-\d{2}-\d{2}$/s', I('post.end_time')) && $this->error('结束时间不能为空或结束时间格式不对');
            getStatus('hours', I('post.end_time_hour')) == null && $this->error('请选择结束时间的小时时间段');
            
            $start_time = strtotime(I('post.start_time') . " " . getStatus('hours', I('post.start_time_hour')) . ":00");
            $end_time = strtotime(I('post.end_time') . " " . getStatus('hours', I('post.end_time_hour')) . ":00");
            
            $start_time >= $end_time && $this->error('结束时间不能低于初始时间');
            
            $M_special_panic = M('special_panic');
            $result=$M_special_panic->order('end_time desc')->find();

            // date('H:i:s',$result['end_time']);
            
            $start_time < strtotime(date('Y-m-d')." ".date('H:i:s',$result['end_time'])) && $this->error('开始时间必须在上一场结束时间之后');

            $data = array(
                'start_time' => $start_time,
                'end_time' => $end_time,
                'create_time' => NOW_TIME,
                'update_time' => NOW_TIME,
                'status' => 1
            );
            
            $id = $M_special_panic->add($data);
            
            $id ? $this->success('新增成功', U('panic')) : $this->error('新增失败，未知错误');
        } else {
            $this->meta_title = '新增抢购会时间表';
            $this->display('editpanic');
        }
    }
    
    /**
     * 编辑抢购会时间表
     *
     * @param integer id 编号
     * @author Ringo Chan <334258471@qq.com>
     */
    public function editpanic(){
        $id = I('get.id', 0);
        
        $M_special_panic = M('special_panic');
        $info = $M_special_panic->find($id);
        
        !$info && $this->error('非法参数ID');
        
        if (IS_POST) {
            // !preg_match('/^\d{4}-\d{2}-\d{2}$/s', I('post.start_time')) && $this->error('初始时间不能为空或初始时间格式不对');
            getStatus('hours', I('post.start_time_hour')) == null && $this->error('请选择初始时间的小时时间段');
            // !preg_match('/^\d{4}-\d{2}-\d{2}$/s', I('post.end_time')) && $this->error('结束时间不能为空或结束时间格式不对');
            getStatus('hours', I('post.end_time_hour')) == null && $this->error('请选择结束时间的小时时间段');
            
            $start_time = strtotime(I('post.start_time') . " " . getStatus('hours', I('post.start_time_hour')) . ":00");
            $end_time = strtotime(I('post.end_time') . " " . getStatus('hours', I('post.end_time_hour')) . ":00");
            
            $start_time >= $end_time && $this->error('结束时间不能低于初始时间');
            
            $result=$M_special_panic->order('id desc')->find();

            $result_e=$M_special_panic->where('id<'.$id)->order('end_time desc')->find();
            $result_s=$M_special_panic->where('id>'.$id)->order('start_time asc')->find();
            if($result_e){
                $start_time < strtotime(date('Y-m-d')." ".date('H:i:s',$result_e['end_time'])) && $this->error('开始时间必须在上一场结束时间之后');
            }
            if($result_s){
                $end_time > strtotime(date('Y-m-d')." ".date('H:i:s',$result_s['start_time'])) && $this->error('结束时间必须在下一场时间之前');
            }
            
            

            // $start_time < $result['end_time'] && $this->error('开始时间必须在上一场结束时间之后');

            $data = array(
                'start_time' => $start_time,
                'end_time' => $end_time,
                'update_time' => NOW_TIME
            );
            
            $M_special_panic->where(array('id'=>$id))->save($data) ? $this->success('编辑成功', U('panic')) : $this->error('编辑失败，未知错误');
        } else {
            $info['start_time_hour'] = getStatus('hours', intval(time_format($info['start_time'], 'H')));
            $info['end_time_hour'] = getStatus('hours', intval(time_format($info['end_time'], 'H')));
            
            $this->assign('info', $info);
            $this->meta_title = '新增抢购会时间表';
            $this->display('editpanic');
        }
    }
    
    /**
     * 选礼物分类列表
     * 
     * @author Ringo Chan <334258471@qq.com>
     */
    public function gift(){
        $map['status'] = array('egt', 0);
        $list   = $this->lists('special_gift', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '选礼物分类';
        $this->display();
    }
    
    /**
     * 新增选礼物分类
     *
     * @author Ringo Chan <334258471@qq.com>
     */
    public function addgift(){
        if (IS_POST) {
            !preg_match('/[\x{4e00}-\x{9fa5}A-Za-z]{2,60}+$/u', I('post.title')) && $this->error('分类标题长度必须在3-60个字符之间');
            $cover_id=I('post.cover_id');
            empty($cover_id) && $this->error('图片不能为空');
            !is_numeric(I('post.sort')) && $this->error('排序必须为数字');
            
            $M_special_gift = M('special_gift');
            
            $data = array(
                'title' => I('post.title'),
                'cover_id' => I('post.cover_id'),
                'sort' => I('post.sort'),
                'create_time' => NOW_TIME,
                'update_time' => NOW_TIME,
                'status' => 1
            );
            
            $id = $M_special_gift->add($data);
            
            $id ? $this->success('新增成功', U('gift')) : $this->error('新增失败，未知错误');
        } else {
            $this->meta_title = '新增选礼物分类';
            $this->display('editgift');
        }
    }
    
    /**
     * 编辑选礼物分类
     * 
     * @param integer id 编号
     * @author Ringo Chan <334258471@qq.com>
     */
    public function editgift(){
        $id = I('get.id', 0);
        
        $M_special_gift = M('special_gift');
        $info = $M_special_gift->find($id);
        
        !$info && $this->error('非法参数ID');
        
        if (IS_POST) {
            !preg_match('/[\x{4e00}-\x{9fa5}A-Za-z]{2,60}+$/u', I('post.title')) && $this->error('分类标题长度必须在3-60个字符之间');
            $cover_id = I('post.cover_id');
            empty($cover_id) && $this->error('图片不能为空');
            !is_numeric(I('post.sort')) && $this->error('排序必须为数字');
    
            $data = array(
                'title' => I('post.title'),
                'cover_id' => I('post.cover_id'),
                'sort' => I('post.sort'),
                'update_time' => NOW_TIME
            );
            
            $M_special_gift->where(array('id'=>$id))->save($data) ? $this->success('编辑成功', U('gift')) : $this->error('编辑失败，未知错误');
        } else {
            $this->assign('info', $info);
            $this->meta_title = '编辑选礼物分类';
            $this->display('editgift');
        }
    }
    
    /**
     * 竞拍会场
     */
    public function  Paipinactivity(){
        
        $list=$this->lists('product_special_activity');
        $list=int_to_string($list);
        $this->assign('_list',$list);
        $this->meta_title = '竞拍会场';
        $this->display();
    }
    
    /**
     * 编辑会场
     */
    public function  editpaipinactivity(){
        
        $id = I('get.id', 0);        
        $M_paipin = M('product_special_activity');
       
        
        if (IS_POST) {
            !preg_match('/[\x{4e00}-\x{9fa5}A-Za-z]{2,60}+$/u', I('post.title')) && $this->error('标题长度必须在3-60个字符之间');
           
            !is_numeric(I('post.sort')) && $this->error('排序必须为数字');
    
            $data = array(
                'title' => I('post.title'),
                'short_desc' => I('post.short_desc'),
                'sort' => I('post.sort'),
                'update_time' => NOW_TIME,
                'create_time'=>NOW_TIME
            );
            if($id){
            $M_paipin->where(array('id'=>$id))->save($data) ? $this->success('编辑成功', U('paipinactivity')) : $this->error('编辑失败，未知错误');
            }
            else{
                $M_paipin->add($data) ? $this->success('新增成功', U('paipinactivity')) : $this->error('新增失败，未知错误');
            }
            
        } else {
            if($id){
            $info = $M_paipin->find($id);
            
            !$info && $this->error('非法参数ID');
            $this->assign('info', $info);
            }
           
            $this->meta_title = '竞拍专场';
            $this->display();
        }
    }
    
    /**
     * 通用删除
     * 
     * @param string method 方法名
     * @param array id 编号
     * @author Ringo Chan <334258471@qq.com>
     */
    public function delete($method=null){
        $map['id'] =   array('in',I('id', 0));
        $feedback = false;
        
        switch ( strtolower($method) ){
            case 'gift':
                $M_special_gift = M('special_gift');
                $feedback = $M_special_gift->where($map)->delete();
                break;
            case 'paipinactivity':
                $M_paipin_activity = M('paipin_activity');
                $feedback = $M_paipin_activity->where($map)->delete();
                break;  
            case 'panic':
                $feedback = M('special_panic')->where($map)->delete();
                break;
            default:
                $this->error('参数非法');
        }
        
        $feedback !== false ? $this->success('删除成功！') : $this->error('删除失败！');
    }
    
    /**
     * 通用改变状态
     * 
     * @param string $method 方法名
     * @param number $status 状态值
     * @param array id 编号
     * @author Ringo Chan <334258471@qq.com>
     */
    public function changeStatus($method=null, $status=0){
        $map['id'] =   array('in',I('id', 0));
        $feedback = false;
        
        switch ( strtolower($method) ){
            case 'gift':
                $M_special_gift = M('special_gift');
                $data['status'] = $status;
                $feedback = $M_special_gift->where($map)->save($data);
                break;
            case 'paipinactivity':
                 $M_paipin_activity = M('paipin_activity');
                $data['status'] = $status;
                $feedback = $M_paipin_activity->where($map)->save($data);
                break;
            default:
                $this->error('参数非法');
        }
        
        $feedback !== false ? $this->success('状态修改成功！') : $this->error('状态修改失败！');
    }
    
    
}
?>